# -*- coding: utf-8 -*-
"""
收集任何python程序的报错
"""

MAGIC_CUBE_URL = ""
MAGIC_CUBE_TOKEN = ""
MAGIC_CUBE_VERSION = ""

def send_error(error_type, code, request):
    url = MAGIC_CUBE_URL + "/api/errors/error/"
    token = MAGIC_CUBE_TOKEN
    version = MAGIC_CUBE_VERSION
    meta = request.META
    if not version:
        try:
            version = os.popen("git rev-parse HEAD").read()
        except:
            version = "dev"

    params = {
        "token": token,
        "code": code,
        "errorType": error_type,
        "clientUser": "",
        "version": version,
    }

    data = {}

    data["ip"] = ip
    data["user_agent"] = ""
    data["path"] = ""
    data["query_string"] = ""
    data["body"] = ""
    data["method"] = ""
    params["jsonData"] = ""

    data = json.dumps(params)
    data = bytes(data, "utf-8")
    request = urllib.request.Request(url, data=data)
    response = urllib.request.urlopen(request)